<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" language="javascript">
<!--
	function selectSubmit(){
		var warningItem = "<c:out value='${warningItem}'/>";
		var checkedUserIds = "";
		var rObjs = document.getElementsByName("roleId");
		for(var i = 0; i < rObjs.length; i++){
			if(rObjs[i].checked){
				var uObjs = document.getElementsByName("u_"+rObjs[i].value);
				for(var j = 0; j < uObjs.length; j++){
					if(uObjs[j].checked){
						checkedUserIds = checkedUserIds + uObjs[j].value + ",";
					}
				}
			}
		}
		if(checkedUserIds == ""){
			alert("请选择预警对象！");
		}else{
			window.returnValue = warningItem + "##" + checkedUserIds;
			self.close();
		}
	}
	
	function childSelect(roleId){
		var uObjs = document.getElementsByName("u_"+roleId);
		var sign = 0;
		for(var i = 0; i < uObjs.length; i++){
			if(uObjs[i].checked){
				sign = sign + 1;
			}
		}
		if(sign <= 0){
			document.getElementById(roleId).checked = false;
		}else{
			document.getElementById(roleId).checked = true;
		}
	}
	
	function initPage(){
		var warningObjects = "<c:out value='${warningObjects}'/>";
		var nodes = document.warningObjectForm.elements;
		if(warningObjects != "" && warningObjects != "0"){
			var warningObjects = warningObjects.split(",");
			for(var i = 0; i < warningObjects.length; i++){
				for(var j = 0; j < nodes.length; j++){
					var node = document.warningObjectForm.elements[j];
					if(node.type == "checkbox" && node.id == "userId" && node.value == warningObjects[i]){
						node.checked = true;
						var str = (node.name).split("_");
						if(str[1] != ""){
							document.getElementById(str[1]).checked = true;
						}
					}
				}
			}
		}
	}
	
-->
</script>
</head>
<body onload="initPage()">
<form name="warningObjectForm" action="" method="post">
<br>
	<div class="tools">
		<input class="btn1" type="button" value="确 定" onclick="selectSubmit()"/>&nbsp;&nbsp;
		<input class="btn1" type="button" value="取 消" onclick="window.close();"/>&nbsp;&nbsp;
	</div>
	
	<table class="table_schedule" style="text-align: center;">
		<tr>
			<td class="title" colspan="3">
				预警项目：
			<c:choose>
				<c:when test="${warningItem == '1'}">员工生日</c:when>
				<c:when test="${warningItem == '2'}">审批流程提醒</c:when>
				<c:when test="${warningItem == '3'}">客户生日</c:when>
				<c:when test="${warningItem == '4'}">日程管理提醒</c:when>
				<c:when test="${warningItem == '5'}">拜访提醒</c:when>
				<c:when test="${warningItem == '6'}">公海规则提醒</c:when>
				<c:when test="${warningItem == '7'}">合同到期提醒</c:when>
			</c:choose>
			</td>
		</tr>
		<tr>
			<td class="title4">角色</td>
			<td class="title4">用户</td>
			<td class="title4" width="15%">选择</td>
		</tr>
		<c:forEach var="role" items="${roles}">
			<c:forEach var="user" items="${role.users}" begin="0" end="0">
			<tr>
				<td rowspan="${fn:length(role.users)}" align="left">
					<c:if test="${fn:length(role.users) > 0}">
					<input id="${role.id}" name="roleId" type="checkbox" value="${role.id}" onclick="checkAll('u_${role.id}')"/>${role.roleName}
					</c:if>
				</td>
				<td align="left">${user.userName}</td>
				<td><input id="userId" name="u_${role.id}" type="checkbox" value="${user.id}" onclick="childSelect(${role.id})"/></td>
			</tr>
			</c:forEach>
			<c:forEach var="user" items="${role.users}" begin="1">
			<tr>
				<td align="left">${user.userName}</td>
				<td><input id="userId" name="u_${role.id}" type="checkbox" value="${user.id}" onclick="childSelect(${role.id})"/></td>
			</tr>
			</c:forEach>
		</c:forEach>
	</table>
</form>	
</body>
</html>